package gui;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

import network.LoginWorker;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import data.LoginResponse;

public class LoginPanel extends JPanel {
	private static final long serialVersionUID = -9025868930601154473L;
	private JLabel lblNazwaUzytkownika;
	private JTextField nameTextField;
	private JLabel lblHaslo;
	private JPasswordField passTextField;
	private JButton btnLogin;
	private AppletMenuBarDemo mParent;
	private JPanel panel_3;

	/**
	 * Create the panel.
	 */
	public LoginPanel(AppletMenuBarDemo parent) {

		mParent = parent;
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		panel_3 = new JPanel();
		panel_3.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		panel_3.setMaximumSize(new Dimension(250, 100));
		panel_3.setPreferredSize(new Dimension(200, 100));
		panel_3.setMinimumSize(new Dimension(200, 100));
		panel_3.setLayout(new FormLayout(new ColumnSpec[] { FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
				FormFactory.MIN_COLSPEC, FormFactory.LABEL_COMPONENT_GAP_COLSPEC, FormFactory.GROWING_BUTTON_COLSPEC,
				FormFactory.LABEL_COMPONENT_GAP_COLSPEC, }, new RowSpec[] { FormFactory.LINE_GAP_ROWSPEC,
				RowSpec.decode("20px"), FormFactory.LINE_GAP_ROWSPEC, RowSpec.decode("23px"),
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, }));

		lblNazwaUzytkownika = new JLabel("Nazwa u\u017Cytkownika:");
		panel_3.add(lblNazwaUzytkownika, "2, 2, right, center");

		nameTextField = new JTextField("karlkar");
		panel_3.add(nameTextField, "4, 2, fill, top");
		nameTextField.setColumns(10);

		lblHaslo = new JLabel("Has\u0142o:");
		panel_3.add(lblHaslo, "2, 4, left, center");

		passTextField = new JPasswordField("whatever");
		panel_3.add(passTextField, "4, 4, fill, center");
		passTextField.setColumns(10);
		passTextField.addKeyListener(new KeyListener() {

			@Override
			public void keyTyped(KeyEvent e) {
			}

			@Override
			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					btnLogin.requestFocus();
					btnLogin.doClick();
				}
			}

			@Override
			public void keyPressed(KeyEvent e) {
			}
		});

		Box box = new Box(BoxLayout.Y_AXIS);
		box.setAlignmentX(JComponent.CENTER_ALIGNMENT);
		box.add(Box.createVerticalGlue());
		box.add(panel_3);

		btnLogin = new JButton("Zaloguj");
		btnLogin.setMinimumSize(new Dimension(120, 25));
		btnLogin.setPreferredSize(new Dimension(120, 25));
		btnLogin.setMaximumSize(new Dimension(120, 25));
		panel_3.add(btnLogin, "2, 6, 3, 1, center, top");

		btnLogin.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (nameTextField.getText().equals("") || passTextField.getPassword().length == 0) {
					JOptionPane.showMessageDialog(LoginPanel.this,
							"Musisz poda\u0107 swoj\u0105 nazw\u0119 u\u017cytkownika i has\u0142o.",
							"B\u0142\u0105d logowania", JOptionPane.ERROR_MESSAGE);
					return;
				}

				mParent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
				nameTextField.setEditable(false);
				passTextField.setEditable(false);
				btnLogin.setEnabled(false);

				new LoginWorker(LoginPanel.this).execute();
			}
		});
		box.add(Box.createVerticalGlue());
		add(box);
	}

	public String getLogin() {
		return nameTextField.getText();
	}

	public String getPassword() {
		return passTextField.getText();
	}

	public void clearPassword() {
		passTextField.setText("");
	}

	public void loggedIn(LoginResponse loginStatus) {
		if (loginStatus != null && loginStatus.isSuccessfull())
			mParent.loggedIn(loginStatus.isSuperUser(), nameTextField.getText(), loginStatus.getCompanyInfo().getName()
					+ "<br>" + loginStatus.getCompanyInfo().getAddress1() + "<br>"
					+ loginStatus.getCompanyInfo().getAddress2(), loginStatus.getCompanyInfo().getImage());
		else {
			mParent.setCursor(Cursor.getDefaultCursor());
			JOptionPane.showMessageDialog(LoginPanel.this, loginStatus == null ? "Nieznany b\u0142\u0105d"
					: loginStatus.getMessage(), "B\u0142\u0105d logowania", JOptionPane.ERROR_MESSAGE);
		}
		nameTextField.setEditable(true);
		passTextField.setEditable(true);
		btnLogin.setEnabled(true);
	}
}
